import { getSignatures, getDocList } from '@/api/business'
import ElementUI from 'element-ui'
import axios from 'axios'
// import { mobileExchange } from '@/api'
export default {
	state: {
		info: null
	},

	mutations: {
		SET_INFO(state, info) {
			state.info = info
		}
	},

	actions: {
		async getSignatures({ commit, state, dispatch, getters }) {
			// const { domID, pages } = await _file_url_2domID(
			// 	'http://fubangbrokers.com:8004/upload/20250522/e6423399-dd1e-4d4e-a56a-3878dc64f543.pdf'
			// )
			// var info = {}
			// info.docList = [{ domID, pages }]
			// commit('SET_INFO', info)
			// return
			const { data: info } = await getSignatures(getters.userInfo.businessId)
			const { data: servDocs } = await getDocList(getters.userInfo)

			const fetchDomList = servDocs.map(async (doc) => {
				const { documentFile, documentName } = doc
				const { domID, pages } = await _file_url_2domID(documentFile)
				return { ...doc, domID, pages }
			})
			const docList = await Promise.all(fetchDomList)
			info.docList = docList
			commit('SET_INFO', info)
		}
	}
}

const _file_url_2domID = (fileUrl) => {
	// 上传fileUrl
	// 3.获得选择的文件对象
	return axios({
		url: `${process.env.VUE_APP_SEAL_BASE_PATH}/pdf/upload`,
		method: 'post',
		params: { fileUrl }
	})
		.then((res) => {
			console.log(res)
			if (res.status === 200 && res.data.status === 0) {
				let { id: domID, pages } = res.data.data
				// this.domID = id
				// this.pages = pages
				// this.pagingSeal.show = false
				// this.seals = []
				// this.$router.replace({
				// 	path: this.$route.path,
				// 	query: { id: id, type: this.type }
				// })
				// document.getElementById('excel-upload').value = ''
				// this.$message({
				// 	message: '上传成功',
				// 	type: 'success'
				// })

				return { domID, pages }
			} else {
				throw new Error('服务器返回的status不为0')
			}
		})
		.catch((err) => {
			console.error(err)
			// this.loading = false
			// document.getElementById('excel-upload').value = ''
			// this.$message({
			// 	message: '上传失败',
			// 	type: 'error'
			// })
			ElementUI.Message({
				message: '读取文件失败',
				type: 'error'
			})
		})
}
